//Brian Goodacre

#include "helper_functions.h"

//functions

//for inputs to command interface
void get_choice(int choices[]){
	int return_int=0;
	int i=0;
	
	//get input from file since stdin does not work with MPI
	printf("Reading from file...");
	FILE * file;
	file=fopen("input_read.txt","r");
	
	while(return_int!=-1){
		fscanf(file,"%d",&return_int);
		choices[i]=return_int;
		i++;
	}
	printf("finished reading\n");
}

void print_transcation_file(){
	//from http://www.cplusplus.com/reference/clibrary/cstdio/fread/
	FILE *file;
	long lsize;
	char* buffer;
	size_t result;
	file = fopen("transcation_log.txt","r");
	fseek(file,0,SEEK_END);
	lsize=ftell(file);
	rewind(file);
	buffer=(char*)malloc(sizeof(char)*lsize);
	result = fread(buffer,1,lsize,file);
	fclose(file);
	printf("Comp #\tVer\tTime\t\t\t\tPrevious Data\t\tNew Data\n");
	printf("%s\n",buffer);
	free(buffer);
}
					

void randomize_array(int num_to_change, int *storage_array, int array_length, int MyProc){
	srandom(MyProc*num_to_change+MyProc*storage_array[0]+num_to_change*storage_array[1]);//needed to change the seed
	int j; 
	if(num_to_change==array_length){//change all values in array
		for(j=0;j<array_length;j++){
			storage_array[j]=random()%max_random_number;
		}
	}
	else{//change only a few values
		for(j=0;j<num_to_change;j++){
			storage_array[random()%default_storage_array_size]=random()%max_random_number;
		}
	}
}

//randomize a matrix of values
void randomize_matrix(int storage_matrix[][default_storage_array_size],int num_to_change, int array_length, int num_rows){
	int i;
	for(i=0;i<num_rows;i++){
		randomize_array(num_to_change,storage_matrix[i],array_length,i+2);
	}
}

//randomize a single row of a matrix
void randomize_matrix_array(int storage_matrix[][default_storage_array_size],int num_to_change, int array_length, int num_rows, int proc){
	randomize_array(num_to_change,storage_matrix[proc-2],array_length,proc);
}

//get input
int get_input(){
	int choice;
	do{
		printf("\tchoice: ");
		fflush(stdin);
		scanf("%d",&choice);
		fflush(stdin);
	}while(choice==-1);
	return choice;
}

//log: type = 0 for server	
void print_to_log(int computer_number, int version, int old_storage_array[], int new_storage_array[]){
	FILE *file;
	file = fopen("transcation_log.txt","a+");
	//example log 2 - 12/21/2011
	//computer number
	fprintf(file,"%d\t",computer_number);
	//version number
	fprintf(file,"%d\t",version);
	//time //from http://www.cplusplus.com/reference/clibrary/ctime/asctime/
	time_t rawtime;
	struct tm * timeinfo;
	timeinfo = localtime(&rawtime);
	char time_no_new_line[25];
	strcpy(time_no_new_line,asctime (timeinfo));
	time_no_new_line[24]='\0';
	fprintf(file,"%s\t",time_no_new_line);
	//old data
	int j;
	fprintf(file,"[");
	for(j=0;j<default_storage_array_size;j++){
			fprintf(file,"%d",old_storage_array[j]);
			if(j!=default_storage_array_size-1){
				fprintf(file,",");//print period and new line
			}
	}
	fprintf(file,"]\t");		
	//new data
	fprintf(file,"[");
	for(j=0;j<default_storage_array_size;j++){
			fprintf(file,"%d",new_storage_array[j]);
			if(j!=default_storage_array_size-1){
				fprintf(file,",");//print period and new line
			}
	}
	fprintf(file,"]\n");//end of line
	fclose(file);
}
